package com.main;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Vector;
import java.util.concurrent.CountDownLatch;

import com.dao.MYSQLControl;
import com.model.ProxyIp;
import com.service.ConnectThread;

public class SourceIPCheck {

	public static void main(String[] args) throws Exception{
		List<ProxyIp> iplist = MYSQLControl.executeQueryAll();
		System.out.println("数据库中存有代理ip： "+iplist.size()+" 个");
		
		CountDownLatch latch = new CountDownLatch(iplist.size());
		Vector<ProxyIp> avaliableIp = new Vector<ProxyIp>();
		long t1 = System.currentTimeMillis();
		for(int i = 0; i < iplist.size(); i++){
			ProxyIp proxyIp = iplist.get(i);
			ConnectThread ct = new ConnectThread(proxyIp,latch,avaliableIp);
			ct.start();
		}
		latch.await();
		long t2 = System.currentTimeMillis()-t1;
		System.out.println("校验完毕，数据库代理ip数："+iplist.size()+",  可用:"+avaliableIp.size()+",  耗时:"+t2);
		
		Map<String,String> map = new HashMap<String,String>();
		for(ProxyIp ip:avaliableIp){
			map.put(ip.getIp(), ip.getPort());
		}
		for(ProxyIp ip:iplist){
			if(!map.containsKey(ip.getIp())){
				System.out.println("此代理ip已经无法使用，从数据库中删除："+ip.getIp()+"  "+ip.getPort()+"  "+ip.getLocation());
				MYSQLControl.deleteByMainkey(ip.getIp());
			}
		}

	}

}
